package program.set;

import program.linkedlist.LinkedListV2;

/**
 * 使用链表实现集合
 *
 * 时间复杂度都为: O(n)
 */
public class LinkedListSet<E> implements Set<E> {

    private LinkedListV2<E> list;

    public LinkedListSet() {
        list = new LinkedListV2<>();
    }

    @Override
    public void add(E data) {
        if (!list.contains(data)) {
            // addFirst: O(1)
            // addLast: O(N)
            list.addFirst(data);
        }
    }

    @Override
    public E remove(E data) {
        return list.removeElement(data);
    }

    @Override
    public boolean contains(E data) {
        return list.contains(data);
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }
}
